<template>
  <div class="wrap">
    <template>
      <div>
        <label>设置定位基准面</label>
        <query-list v-model="params.selDatumPlane"
                    :description="$t('message.FrameDesignTool.datumPlane')"
                    :active="params.queryListActiveMap.selDatumPlane"
                    @highlight="highlight($event)"
                    @removeHighlight="removeHighlight($event)"
                    @switchQuery="switchQuery('selDatumPlane')"
                    :tipMsg="$t('message.FrameDesignTool.selDatumPlane')">
        </query-list>
        <div class="d-flex align-items-center">
          <label class="el-form-item__label">{{ $t('message.FrameDesignTool.offsetDirection') }}：</label>
          <el-input v-model="positiveDirection" type="text" v-if="!params.direction" readonly="true"/>
          <el-input v-model="negativeDirection" type="text" v-if="params.direction" readonly="true"/>
          <div class="image-button">
            <img v-if="!params.direction" src="../../../../../webapp/nodeApp/static/image/direction.svg" width="16"
                 height="16" alt="正向" title="正向" @click="switchDirection">
            <img v-if="params.direction" src="../../../../../webapp/nodeApp/static/image/reverse.svg" width="16"
                 height="16" alt="反向" title="反向" @click="switchDirection">
          </div>
        </div>
      </div>
      <div>
        <label>设置外部参考</label>
        <query-list v-model="params.selRefSurface"
                    :description="$t('message.FrameDesignTool.surface')"
                    :active="params.queryListActiveMap.selRefSurface"
                    @highlight="highlight($event)"
                    @removeHighlight="removeHighlight($event)"
                    @switchQuery="switchQuery('selRefSurface')"
                    :tipMsg="$t('message.FrameDesignTool.selRefSurface')">
        </query-list>
      </div>
      <div>
        <label>设置截面选型参数：</label>
        <div class="d-flex align-items-center">
          <label class="el-form-item__label">{{ $t('message.FrameDesignTool.externalFrameLength') }}</label>
          <base-input-number v-model="params.externalFrameLength"
                             :min="params.bottomFrameThickness"
                             :max="params.offset"
                             @change="changeCommon"/>
          <label class="el-form-item__label">mm</label>
        </div>
        <div class="d-flex align-items-center">
          <label class="el-form-item__label">{{ $t('message.FrameDesignTool.externalFrameThickness') }}</label>
          <base-input-number v-model="params.externalFrameThickness"
                             :min="0.1"
                             :max="90"
                             @change="changeCommon"/>
          <label class="el-form-item__label">mm</label>
        </div>
        <div class="d-flex align-items-center">
          <label class="el-form-item__label">{{ $t('message.FrameDesignTool.bottomFrameLength') }}</label>
          <base-input-number v-model="params.bottomFrameLength"
                             :min="0.1"
                             :max="90"
                             @change="changeCommon"/>
          <label class="el-form-item__label">mm</label>
        </div>
        <div class="d-flex align-items-center">
          <label class="el-form-item__label">{{ $t('message.FrameDesignTool.bottomFrameThickness') }}</label>
          <base-input-number v-model="params.bottomFrameThickness"
                             :min="0.1"
                             :max="90"
                             @change="changeCommon"/>
          <label class="el-form-item__label">mm</label>
        </div>
        <div class="d-flex align-items-center">
          <label class="el-form-item__label">{{ $t('message.FrameDesignTool.internalFrameLength') }}</label>
          <base-input-number v-model="params.internalFrameLength"
                             :min="params.bottomFrameThickness"
                             :max="params.offset"
                             @change="changeCommon"/>
          <label class="el-form-item__label">mm</label>
        </div>
        <div class="d-flex align-items-center">
          <label class="el-form-item__label">{{ $t('message.FrameDesignTool.internalFrameThickness') }}</label>
          <base-input-number v-model="params.internalFrameThickness"
                             :min="0.1"
                             :max="90"
                             @change="changeCommon"/>
          <label class="el-form-item__label">mm</label>
        </div>
      </div>
    </template>
    <div class="btn">
      <el-button type="primary" size="small" :disabled="!isConfirm"
                 @click="confirm">{{ $t('message.FrameDesignTool.create') }}
      </el-button>
      <el-button size="small"
                 @click="cancel">{{ $t('message.FrameDesignTool.close') }}
      </el-button>
    </div>
  </div>
</template>

<script>
import { frameDesignToolCommand } from "../../../Command/FrameDesignToolCommand";
import { ElementType } from "../../../modeling/TypeDefine";
import DialogMixin from "../DialogMixin";

export default {
  name: "CFrame",
  mixins: [DialogMixin],
  data() {
    return {
      isInit: false,
      positiveDirection: "正向",
      negativeDirection: "负向",
      params: {
        filters: [ElementType.DATUMPLANE],
        direction: false,
        reverse: 0,
        offset: 1,//裁切距离
        selDatumPlane: [],//定位基准面
        offsetDatumPlane: [],//偏移基准面
        selRefSurface: [],//参考曲面
        copySelRefSurface: [],//复制参考曲面列表
        bottomFrameLength: 3,//框底长度
        internalFrameLength: 2,//内框长度
        externalFrameLength: 2,//外框长度
        bottomFrameThickness: 1,//框底厚度
        internalFrameThickness: 1,//内框厚度
        externalFrameThickness: 1,//外框厚度
        queryListActiveMap: {
          //表示在哪个query上生效
          selRefSurface: 0,//参考曲面,
          selDatumPlane: 1,//定位基准面
        }
      }
    }
  },
  activated() {
    this.changePluginContainerHeight();
  },
  methods: {
    changePluginContainerHeight() {
      frameDesignToolCommand.changePluginContainerHeight({
        minWidth: this.$el.clientWidth,
        maxHeight: this.$el.clientHeight + 10
      });
    },

    cancel() {
      frameDesignToolCommand.clearAllHighLight();
      frameDesignToolCommand.closeActiveCmd();
    },

    handlePickElement(res) {
      if (this.params.queryListActiveMap.selDatumPlane === 1) {
        this.params.selDatumPlane = [];
        res?.element.forEach(ele => {
          let datumInfo = {
            id: ele?.element.id,
            name: ele.element?.params?.planeName,
            type: ele?.element
          }
          this.params.selDatumPlane.push(datumInfo);
        });
      } else if (this.params.queryListActiveMap.selRefSurface === 1) {
        this.params.selRefSurface = [];
        res?.element.forEach(ele => {
          let datumInfo = {
            id: ele?.element.id,
            name: ele.element?.name,
            type: ele?.element
          }
          this.params.selRefSurface.push(datumInfo);
        });
      }
    },

    switchQuery(name) {
      frameDesignToolCommand.resetSelectType();
      this.setSelectType(name);
      for ( const key in this.params.queryListActiveMap ) {
        if (key === name) {
          this.params.queryListActiveMap[key] = 1;
        } else {
          this.params.queryListActiveMap[key] = 0;
        }
      }
    },

    setSelectType(typeName) {
      if (typeName === 'selDatumPlane') {
        this.params.filters = [ElementType.DATUMPLANE];
      } else if (typeName === 'selRefSurface') {
        this.params.filters = [ElementType.SURFACE, ElementType.FACE];
      }
      frameDesignToolCommand.setSelectType(this.params.filters);
    },
    getCurrentQueryActive() {
      if (this.params.queryListActiveMap.selDatumPlane === 1) {
        this.params.filters = [ElementType.DATUMPLANE];
      } else if (this.params.queryListActiveMap.selRefSurface === 1) {
        this.params.filters = [ElementType.SURFACE, ElementType.FACE];
      }
      frameDesignToolCommand.setSelectType(this.params.filters);
    },

    changeCommon() {

    },

    confirm() {
      if (this.params.selDatumPlane.length === 0) {
        let msg = "请先择定位基准面";
        frameDesignToolCommand.eventBusAlertMsg(msg, 'error');
        return;
      }
      if (this.params.selRefSurface.length === 0) {
        let msg = "请先选择外部参考曲面";
        frameDesignToolCommand.eventBusAlertMsg(msg, 'error');
        return;
      }
      if (this.addition(this.params.externalFrameThickness, this.params.internalFrameThickness) > this.params.bottomFrameLength) {
        let msg = "请保持内外框厚度和(δ1+δ2)小于底框长度B";
        frameDesignToolCommand.eventBusAlertMsg(msg, 'error');
        return;
      }
      if (Number(this.params.externalFrameLength) >= Number(this.params.internalFrameLength)) {
        this.params.offset = this.params.externalFrameLength;
      }
      if (Number(this.params.internalFrameLength) > Number(this.params.externalFrameLength)) {
        this.params.offset = this.params.internalFrameLength;
      }
      frameDesignToolCommand.createDatumByDistance(this.params.selDatumPlane[0].id, Number(this.params.offset), this.params.reverse, (res, featureId) => {
        this.params.offsetDatumPlane = res;
        //复制曲面
        let surfaceRefIds = this.params.selRefSurface?.map(item => item.id);
        frameDesignToolCommand.getElementsEntityIdsByIds(surfaceRefIds, res => {
          let entityRefIds = res;
          frameDesignToolCommand.copySurface(surfaceRefIds, entityRefIds, res => {
            let copySurfaceData = res;
            if (copySurfaceData?.code === 200) {
              frameDesignToolCommand.query.getElementsByFeatureId(copySurfaceData?.data?.modelData?.currentFeature[0].id).then(res => {
                this.params.copySelRefSurface = res?.data?.elements?.filter(item => item.type === ElementType.FACE);
                if (this.params.copySelRefSurface.length) {
                  this.previewTrimSurface();
                }
              })
            }
          });
        });
      })
    },
    previewTrimSurface() {
      let surface = this.params.copySelRefSurface;
      let trimEle = [...this.params.offsetDatumPlane, ...this.params.selDatumPlane];
      let trimEles = trimEle.map(e => e.id);
      frameDesignToolCommand.previewTrimSurface(surface, trimEle, async (res) => {
        this.params.newSrfData = [];
        this.params.oldSrfData = [];
        this.params.newSrfData = res.data.elements.newSrfData;
        this.params.oldSrfData = res.data.elements.oldSrfData;
        let surfaceIds = res.data.elements.newSrfData;
        let getKeepSurface = [];//获取符合条件的保留面
        for ( const id of surfaceIds ) {
          let measure = [];
          for ( const item of trimEles ) {
            let isCross = await frameDesignToolCommand.getMeasure([id, item]);
            measure.push(isCross);
          }
          if (measure.every(item => item === true)) {
            getKeepSurface.push(id);
          }
        }
        this.params.keepTags = [];
        let keepTags = new Array(this.params.newSrfData.length).fill(0);
        for ( let i = 0; i < this.params.newSrfData.length; i++ ) {
          if (getKeepSurface.includes(this.params.newSrfData[i])) {
            keepTags[i] = 1;
          }
        }
        this.params.keepTags = keepTags;
        let params = {
          offset: Number(this.params.offset),
          bFrameLength: Number(this.params.bottomFrameLength),
          inFrameLength: Number(this.params.internalFrameLength),
          exFrameLength: Number(this.params.externalFrameLength),
          inFrameThickness: Number(this.params.internalFrameThickness),
          exFrameThickness: Number(this.params.externalFrameThickness),
          bFrameThickness: Number(this.params.bottomFrameThickness),
          // offsetDatumPlane: this.params.offsetDatumPlane,
          selDatumPlane: this.params.selDatumPlane,
          trimEle: [...this.params.offsetDatumPlane, ...this.params.selDatumPlane],
          selRefSurface: this.params.copySelRefSurface,
          keepTags: this.params.keepTags,
          newSrfData: this.params.newSrfData,
          oldSrfData: this.params.oldSrfData,
          frameType: 1,
        }
        await frameDesignToolCommand.createCFrame(params);
      });
    },
    switchDirection() {
      this.params.direction = !this.params.direction;
      switch (this.params.direction) {
        case false:
          this.params.reverse = 0;
          break;
        case true:
          this.params.reverse = 1;
          break;
      }
    },

    addition(a, b) {
      return Number(a) + Number(b);
    }
  },
  computed: {
    isConfirm() {
      if (this.params.selDatumPlane.length === 1 && this.params.selRefSurface.length === 1) {
        return 1;
      } else {
        return 0;
      }
    }
  }
}
</script>

<style scoped>
.wrap {
  font-size: 12px;
}

.dialog-row {
  display: flex;
  align-items: center;
  margin: 7px 0;
  height: 40px;
}

.image-button {
  width: 23px;
  height: 18px;
  margin-left: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  background-color: var(--bg-color9);
  box-shadow: 0 2px 2px 1px var(--bg-color8);
  transition: all .3s;
  transform: translateY(0);
  cursor: pointer;
}

</style>